﻿using System;
using System.Collections.Generic;
using System.Linq;
using Mefisto4.Core.Models;
using Mefisto4.DataAccess.Helpers;

namespace Mefisto4.BusinessObjects
{
    public class UserBusiness : IDisposable
    {
        #region Business Methods

        public IEnumerable<SimpleUser> GetUsers()
        {
            List<SimpleUser> users;

            using (var dbAccess = new Mefisto4DataAccessSqlCE())
            {
                users = dbAccess.ReadEntities<SimpleUser>("select u.UserName as Login, " +
                                                          "         m.Email as Email, " +
                                                          "         m.IsApproved as IsApproved, " +
                                                          "         m.IsLockedOut as IsLockedOut, " +
                                                          "         m.CreateDate as CreateDate, " +
                                                          "         m.LastLoginDate as LastLoginDate, " +
                                                          "         m.Comment as Comment " +
                                                          "from Users u " +
                                                          "inner join Memberships m " +
                                                          "         on u.UserId = m.UserId " +
                                                          "order by u.UserName ", null).ToList();
            }

            return users;
        }

        public SimpleUser GetUser(string login)
        {
            SimpleUser user;

            using (var dbAccess = new Mefisto4DataAccessSqlCE())
            {
                user = dbAccess.ReadEntity<SimpleUser>(string.Format("select u.UserName as Login, " +
                                                                     "         m.Email as Email, " +
                                                                     "         m.IsApproved as IsApproved, " +
                                                                     "         m.IsLockedOut as IsLockedOut, " +
                                                                     "         m.CreateDate as CreateDate, " +
                                                                     "         m.LastLoginDate as LastLoginDate, " +
                                                                     "         m.Comment as Comment " +
                                                                     "from Users u " +
                                                                     "inner join Memberships m " +
                                                                     "         on u.UserId = m.UserId " +
                                                                     "where u.UserName = {0} " +
                                                                     "order by u.UserName ", login), null);
            }

            return user;
        }

        #endregion

        #region IDisposable Method

        public void Dispose()
        {
            
        }

        #endregion
    }
}